package com.example.bysj2020.utils;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

/**
 * 动画工具类
 * UpdateListener： 动画过程中通过添加此监听来回调数据
 * EndListener： 动画结束的时候通过此监听器来做一些处理
 */
public class AnimUtil {
	private ValueAnimator valueAnimator;
	private UpdateListener updateListener;
	private EndListener endListener;
	private long duration;
	private float start;
	private float end;
	private Interpolator interpolator = new LinearInterpolator();
	public AnimUtil() {
		duration = 1000; //默认动画时常1s
		start = 0.0f;
		end = 1.0f;
		interpolator = new LinearInterpolator();// 匀速的插值器
	}
	public void setDuration(int timeLength) {
		duration = timeLength;
	}
	public void setValueAnimator(float start, float end, long duration) {
		this.start = start;
		this.end = end;
		this.duration = duration;
	}
	public void setInterpolator(Interpolator interpolator) {
		this.interpolator = interpolator;
	}

	public void startAnimator() {
		if (valueAnimator != null){
			valueAnimator = null;
		}
		valueAnimator = ValueAnimator.ofFloat(start, end);
		valueAnimator.setDuration(duration);
		valueAnimator.setInterpolator(interpolator);
		valueAnimator.addUpdateListener(valueAnimator -> {
			if (updateListener == null) {
				return;
			}
			float cur = (float) valueAnimator.getAnimatedValue();
			updateListener.progress(cur);
		});
		valueAnimator.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {}
			@Override
			public void onAnimationEnd(Animator animator) {
				if(endListener == null){
					return;
				}
				endListener.endUpdate(animator);
			}
			@Override
			public void onAnimationCancel(Animator animator) {}
			@Override
			public void onAnimationRepeat(Animator animator) {}
		});
		valueAnimator.start();
	}
	public void addUpdateListener(UpdateListener updateListener) {
		this.updateListener = updateListener;
	}
	public void cancelAnimator(){
		valueAnimator.cancel();
	}
	public void addEndListner(EndListener endListener){
		this.endListener = endListener;
	}
	public interface EndListener {
		void endUpdate(Animator animator);
	}
	public interface UpdateListener {
		void progress(float progress);
	}

}
